<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * HITI AdminCP
 *
 * @package     HITI AdminCP Project
 * @since       Monday, October 21, 2013, 6:00 PM
 * @final       
 * @category    Controllers
 * @see 		Regions
 * @author      PHUOCNGUYEN - HITI Dev Team
 * @copyright   Copyright (c) 2013, HITI Corp
 */

class Regions extends CI_Controller {
	var $q = '';
	var $updated = '';
	var $failed = '';
	var $name = '';
	var $error = '';
	var $uri_arr = array();
	var $uri_str = '';	
	
	public function __construct()
	{
		parent::__construct();
		$this->load->model('hiti_model', 'hiti', true);
		$this->hiti->load_config();
		if (check_rights() == false)
		{
			redirect();
		}
		$this->load->model($GLOBALS['var']['act'].'_model', 'Reg');		
		$this->q = $this->input->get('q', true);
		$this->updated = $this->input->get('updated', true);
		$this->failed = $this->input->get('failed', true);
		$this->name = $this->input->get('name', true);
		$this->error = $this->input->get('error', true);
		$this->uri_arr = array(
			'deleted' => $GLOBALS['var']['deleted'], 
			'q' => $this->q,
			'rowstart' => ''
		);
		$this->uri_str = url_uri($this->uri_arr);
	}

	public function index()
	{
		/*
		* Truyen thuoc tinh
		*/
		$methods =& get_instance();
		/*
		* Xu ly du lieu
		*/
		$data = array(
			'updated' => $this->updated,
			'failed' => $this->failed,
			'name' => $this->name,
			'uri_str' => $this->uri_str,
			'url_update' => site_url($GLOBALS['var']['act'].'/update'),
			'regions' => array(),
			'methods' => $methods
		);
		$rows = $this->Reg->show($this->q);
		if ($rows) $data['regions'] = $rows;
		/*
		* Hien thi
		*/
		$header = array(
			'title' => 'Khu vực giao hàng',
			'add_link' => current_url().'/update',
			'search' => true,
			'page_list' => '',
			'datetime_picker' => false,
			'submit_btn' => false,
			'cat_list' => array(),
			'uri' => $this->uri_arr,
			'act' => $GLOBALS['var']['act'],
			'do' => $GLOBALS['var']['do'],
			'id' => $GLOBALS['var']['id'],
			'filter_cat' => $GLOBALS['var']['filter_cat']
		);
		$this->load->view('header', $header);
		$this->load->view($GLOBALS['var']['act'].'/index', $data);
		$this->load->view('footer');
	}

	public function show_regions($cats, $parent = 0, $level = '')
	{
		global $i, $root;
		if (empty($i)) $i = 1;
		if (empty($root)) $root = '';
		foreach ($cats as $cat)
		{
			if ($cat['parent'] == $parent)
			{
				$root_id = '';
				if ($cat['parent'] == 0) $root = $cat['id'];
				else $root_id = $root;
				$data = array(
					'info' => $cat,
					'uri_str' => $this->uri_str,
					'stt' => $i++,
					'root_id' => $root_id,
					'level' => $level
				);
				$this->load->view($GLOBALS['var']['act'].'/row', $data);
				if ($cat['chirld']) $this->show_regions($cats, $cat['id'], $level.'<div class="sub_item"></div>');
			}
		}
	}

	public function update($id = '')
	{
		$info = $this->Reg->info($id);
		$data = array(
			'categories' => $this->Reg->show($this->q),
			'updated' => $this->updated,
			'failed' => $this->failed,
			'name' => $this->name,
			'info' => $info,
			'action' => site_url($GLOBALS['var']['act'].'/process').$this->uri_str
		);
		/*
		* Hien thi
		*/
		$header = array(
			'title' => ($id ? 'Cập nhập' : 'Thêm mới').' khu vực',
			'add_link' => '',
			'search' => false,
			'page_list' => '',
			'datetime_picker' => false,
			'submit_btn' => true,
			'cat_list' => array(),
			'uri' => $this->uri_arr,
			'act' => $GLOBALS['var']['act'],
			'do' => $GLOBALS['var']['do'],
			'id' => $GLOBALS['var']['id'],
			'filter_cat' => $GLOBALS['var']['filter_cat']
		);
		$this->load->view('header', $header);
		$this->load->view($GLOBALS['var']['act'].'/update', $data);
		$this->load->view('footer');
	}

	public function process()
	{
		/*
		* Kiem tra POST method
		*/
		if(!$_POST)
		{
			redirect();
		}
		/*
		* Kiem tra token va tham so yeu cau
		*/
		$name_vn = $this->input->post('name_vn', true);
		if(!token_validation() || $name_vn == '')
		{
			redirect($GLOBALS['var']['act']);
		}
		/*
		* Them tham so url
		*/
		$this->uri_arr['name'] = str_replace('&', '', $name_vn);
		/*
		* Xu ly du lieu
		*/
		$id = $this->input->post('id', true);
		if ((!$id && !$GLOBALS['per']['add']) || ($id && !$GLOBALS['per']['edit']))
		{
			redirect($GLOBALS['var']['act']);
		}
		$data = array(
			'name_vn' => $name_vn,
			'cat' => $this->input->post('region', true),
			'parent' => $this->input->post('cat', true),
			'price' => $this->input->post('price', true),
			'active' => $this->input->post('active', true),
			'keyword' => url_title(viet_decode($name_vn), '-', true),
            'date_added' => date(TIME_SQL),
            'date_modified' => date(TIME_SQL)
		);
		/*
		* Ghi du lieu
		*/
		if ($this->Reg->process($data, $id))
		{
			$this->uri_arr['updated'] = 1;
		}
		else
		{
			$this->uri_arr['failed'] = 1;
		}
		/*
		* Chuyen huong
		*/
		if ($id > 0)
		{
			redirect($GLOBALS['var']['act'].'/update/'.$id.url_uri($this->uri_arr));
		}
		else
		{
			redirect($GLOBALS['var']['act'].url_uri($this->uri_arr));
		}
	}

	public function check()
	{
		/*
		* Kiem tra POST method
		*/
		$id = $this->input->post('id', true);
		$name_vn = $this->input->post('name_vn', true);
		$parent = $this->input->post('parent', true);
		if (!$name_vn)
		{
			echo 1;
			return false;
		}
		echo $this->Reg->check($name_vn, $parent, $id);
		return false;
	}	
}

/* End of file regions_model.php */
/* Location: ./application/controllers/regions_model.php */